﻿using MySql.Data.MySqlClient;
using Oracle.ManagedDataAccess.Client;
using System.Data;
using System.Data.SqlClient;

namespace Common
{
    /// <summary>
    /// 数据库核心对象工厂
    /// </summary>
    public class DbFactory
    {
        /// <summary>
        /// 驱动类型
        /// </summary>
        private static DataProvider DP
        {
            get
            {
                return DataProvider.MySql;
            }
        }

        /// <summary>
        /// 链接字符串
        /// </summary>
        private static string ConStr
        {
            get { return @"Server=127.0.0.1;Database=SOA_Net;Uid=root;Pwd=123123123;"; }
        }

        /// <summary>
        /// 获取Con对象
        /// </summary>
        /// <returns></returns>
        public static IDbConnection GetConnection()
        {
            IDbConnection iDbConnection;
            switch (DP)
            {
                case DataProvider.SqlServer:
                    iDbConnection = new SqlConnection(ConStr);
                    break;
                case DataProvider.Oracle:
                    iDbConnection = new OracleConnection(ConStr);
                    break;
                case DataProvider.MySql:
                    iDbConnection = new MySqlConnection(ConStr);
                    break;
                default:
                    return null;
            }
            return iDbConnection;
        }

        /// <summary>
        /// 获取DataAdapter对象
        /// </summary>
        /// <returns></returns>
        public static IDbDataAdapter GetDataAdapter(string sql, IDbConnection con)
        {
            switch (DP)
            {
                case DataProvider.SqlServer:
                    return new SqlDataAdapter(sql, (SqlConnection)con);
                case DataProvider.Oracle:
                    return new OracleDataAdapter(sql, (OracleConnection)con);
                case DataProvider.MySql:
                    return new MySqlDataAdapter(sql, (MySqlConnection)con);
                default:
                    return null;
            }
        }

        /// <summary>
        /// 数据库驱动类型
        /// </summary>
        private enum DataProvider
        {
            Oracle,
            SqlServer,
            OleDb,
            Odbc,
            MySql
        }
    }
}
